<template>
  <div class="img_card_container" :style="boxStyle">
    <div v-if="state === -1" class="loading_error">
      <svg-icon class="icon" iconClass="tupianjiazaishibai"></svg-icon>
      <p v-if="errorText" class="load_error_text">{{errorText}}</p>
    </div>
    <i v-if="state === 0" class="loading_icon el-icon-loading"></i>
    <div v-if="state === 1" class="loading_success" :style="imgStyle" :class="[preView?'preView_class':'']" @click="previewImage"></div>
  </div>
</template>

<script>
import { transformImgUrl } from '@/filters'
export default {
  components: {},
  props: {
    src: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '100px'
    },
    height: {
      type: String,
      default: '100px'
    },
    borderRadius: {
      type: String,
      default: '0'
    },
    errorText: {
      type: String,
      default: null
    },
    preView: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      state: 0
    }
  },
  watch: {},
  computed: {
    boxStyle () {
      return {
        width: this.width,
        height: this.height,
        borderRadius: this.borderRadius
      }
    },
    imgStyle () {
      return {
        backgroundImage: `url(${transformImgUrl(this.src)})`,
        backgroundSize: 'cover',
        width: this.width,
        height: this.height,
        borderRadius: this.borderRadius
      }
    }
  },
  methods: {
    loading () {
      this.state = 0
      let url = transformImgUrl(this.src)
      if (url === undefined) {
        this.state = -1
        return false
      }
      let image = new Image()
      image.src = url
      image.onload = () => {
        this.state = 1
      }
      image.onerror = () => {
        this.state = -1
      }
    },
    previewImage () {
      if (this.preView) {
        this.$alert(`<img style="width: 100%" src="${transformImgUrl(this.src)}">`, {
          title: '图片预览',
          dangerouslyUseHTMLString: true,
          closeOnClickModal: true,
          confirmButtonText: '关闭',
          center: true,
          callback: () => {}
        }).catch(() => {})
      }
    }
  },
  created () {},
  mounted () {
    this.loading()
  }
}
</script>
<style lang="less" scoped>
.img_card_container
{
  position: relative;
  background-color: #f6f6f6;
  .loading_icon
  {
    position: absolute;
    top: 50%;
    left: 50%;
    font-size: 20px;
    width: 20px;
    height: 20px;
    margin-top: -10px;
    margin-left: -10px;
    color: #999;
  }
  .loading_error
  {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    text-align: center;
    .icon
    {
      font-size: 20px
    }
    .load_error_text
    {
      font-size: 12px;
      color: #999;
      line-height: 20px;
      white-space: nowrap
    }
  }
  .loading_success
  {
    &.preView_class
    {
      cursor: pointer;
      transition: all .2s linear;
      // &:hover
      // {
      //   transform: translateY(-5px);
      //   box-shadow: 0 4px 25px rgba(0,0,0,.5);
      // }
    }
  }
}
</style>
